package com.homestead.utils;

import io.protostuff.*;
import io.protostuff.runtime.RuntimeSchema;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yhb
 * @date 2021-03-17 10:22
 */
public class ProtoStuffUtil {
    private static LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);

    private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<>();
    private static Map<Class<?>, MessageCollectionSchema<?>> cachedListSchema = new HashMap<>();

    /**
     * 只能用来序列化简单的自定义对象,list map等不适用
     * @param obj
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> byte[] serialize(T obj) {
        Class<T> clazz = (Class<T>) obj.getClass();
        Schema<T> schema = getSchema(clazz);
        byte[] data;
        try {
            data = ProtostuffIOUtil.toByteArray(obj, schema, buffer);
        } finally {
            buffer.clear();
        }

        return data;
    }

    /**
     * 只能用来序列化简单的自定义对象,list map等不适用
     * @param data
     * @param clazz 泛型类型
     * @param <V>
     * @return
     */
    public static <V> V deserialize(byte[] data, Class<V> clazz) {
        Schema<V> schema = getSchema(clazz);
        V obj = schema.newMessage();
        ProtostuffIOUtil.mergeFrom(data, obj, schema);
        return obj;
    }

    /**
     * 通过包装对象序列化
     * @param obj
     * @return
     */
    public static byte[] serializeByWrapper(Object obj) {
        ProtoBufWrapper protoBufWrapper = new ProtoBufWrapper(obj);
        Schema<ProtoBufWrapper> schema = getSchema(ProtoBufWrapper.class);
        byte[] data;
        try {
            data = ProtostuffIOUtil.toByteArray(protoBufWrapper, schema, buffer);
        } finally {
            buffer.clear();
        }

        return data;
    }

    /**
     * 通过包装对象反序列化
     * @param bytes
     * @return
     */
    public static <V> V deserializeByWrapper(byte[] bytes) {
        ProtoBufWrapper protoBufWrapper = new ProtoBufWrapper();
        Schema<ProtoBufWrapper> schema = getSchema(ProtoBufWrapper.class);
        ProtostuffIOUtil.mergeFrom(bytes, protoBufWrapper, schema);
        @SuppressWarnings("unchecked")
        V data = (V)protoBufWrapper.getData();
        return data;
    }

    @SuppressWarnings("unchecked")
    private static <T> Schema<T> getSchema(Class<T> cls) {
        Schema<T> schema = (Schema<T>) cachedSchema.get(cls);
        if (Objects.isNull(schema)) {
            schema = RuntimeSchema.getSchema(cls);
            if (Objects.nonNull(schema)) {
                cachedSchema.put(cls, schema);
            }
        }
        return schema;
    }

    /**
     * 包装类，用于list map等的序列化
     */
    private static class ProtoBufWrapper {
        private Object data;

        public ProtoBufWrapper() {
        }

        public ProtoBufWrapper(Object data) {
            this.data = data;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }
    }
}